Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hextof_workflow_steps #169

Merged
merged 61 commits into from
Nov 2, 2023
Merged

Hextof_workflow_steps #169

merged 61 commits into from
Nov 2, 2023

Conversation

steinnymir
Copy link
Member

This PR contains the tof corrections required for hextof, prior to energy calibration.

@steinnymir steinnymir mentioned this pull request Oct 11, 2023
@coveralls
Copy link
Collaborator

coveralls commented Oct 11, 2023

Pull Request Test Coverage Report for Build 6732977899

  • 404 of 467 (86.51%) changed or added relevant lines in 9 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.4%) to 90.103%

Changes Missing Coverage Covered Lines Changed/Added Lines %
sed/loader/utils.py 22 23 95.65%
sed/calibrator/energy.py 86 89 96.63%
sed/loader/flash/loader.py 9 14 64.29%
sed/core/processor.py 6 22 27.27%
sed/loader/flash/utils.py 6 24 25.0%
sed/core/dfops.py 69 89 77.53%
Totals Coverage Status
Change from base Build 6690287002: -0.4%
Covered Lines: 4643
Relevant Lines: 5153

💛 - Coveralls

Copy link
Member

@rettigl rettigl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is early stage, but let me make some general comments. I have the impression we are starting again to develop parallel structures, because of lack of code overview and discussion.
In particular here, I see that develop code for time conversion, that already exists at other places, and introduce duplicates for config entries with different names. I strongly suggest to avoid this, and keep things to a common logic.
I am off course biased because I placed things there first, and am happy to discuss, maybe we should have a separate short meeting to discuss these general design guides if the back and forth on github is too inefficient.

sed/calibrator/hextof.py Outdated Show resolved Hide resolved
sed/calibrator/hextof.py Outdated Show resolved Hide resolved
sed/config/flash_example_config.yaml Outdated Show resolved Hide resolved
sed/config/flash_example_config.yaml Outdated Show resolved Hide resolved
sed/config/flash_example_config.yaml Outdated Show resolved Hide resolved
sed/core/processor.py Outdated Show resolved Hide resolved
sed/core/processor.py Outdated Show resolved Hide resolved
sed/core/processor.py Outdated Show resolved Hide resolved
sed/loader/flash/loader.py Outdated Show resolved Hide resolved
sed/calibrator/hextof.py Outdated Show resolved Hide resolved
sed/calibrator/hextof.py Outdated Show resolved Hide resolved
@rettigl
Copy link
Member

rettigl commented Oct 12, 2023

@steinnymir all these codestyle issues etc. are typically fixed if you run pre-commit. I typically do this manually before commiting. You can also run the pylint and mypy commands manually, to spot potentiall issues before pushing

@rettigl
Copy link
Member

rettigl commented Oct 12, 2023

@steinnymir Regarding momentum calibration/distortion correction: I have the feeling we are again developing parallel structures. This is what we wanted to avoid. The simplified distortion correction you propose has its advantages, but we found it to be notsufficient, because the residual magnetic fields we have in our lens introduce distortions that cannot be described that way. I would also strongly suggest to separate distortion correction and momentum calibration into two steps, as implemented currently. You can implement your correction as alternative to the current one, if you like, but then put it at the same place and integrate it there, so we don't develop parallel structures.

@steinnymir
Copy link
Member Author

@steinnymir all these codestyle issues etc. are typically fixed if you run pre-commit. I typically do this manually before commiting. You can also run the pylint and mypy commands manually, to spot potentiall issues before pushing

I know, the problem is that when I try install pre-commit it creates conflicts in the repo making it unusable. Maybe I can make an other env specific for that. Do you have any suggestions how to do this correctly? do we have a description of the right steps to follow to achieve this, a bit like the readme installation guide?

@steinnymir
Copy link
Member Author

@steinnymir Regarding momentum calibration/distortion correction: I have the feeling we are again developing parallel structures. This is what we wanted to avoid. The simplified distortion correction you propose has its advantages, but we found it to be notsufficient, because the residual magnetic fields we have in our lens introduce distortions that cannot be described that way. I would also strongly suggest to separate distortion correction and momentum calibration into two steps, as implemented currently. You can implement your correction as alternative to the current one, if you like, but then put it at the same place and integrate it there, so we don't develop parallel structures.

These methods we are porting are a direct transfer from hextof-processor or other analysis notebooks we used. The starting point is to get these working so that we can then try merge things together, but still check that results are consistent with our expectations.

The other issue is the complexity of the current calibration methods, which don't make it easy to just jump in and modify methods.

Hopefully the end result is to have a unified method, with features from both worlds working as expected.

@rettigl
Copy link
Member

rettigl commented Oct 12, 2023

@steinnymir all these codestyle issues etc. are typically fixed if you run pre-commit. I typically do this manually before commiting. You can also run the pylint and mypy commands manually, to spot potentiall issues before pushing

I know, the problem is that when I try install pre-commit it creates conflicts in the repo making it unusable. Maybe I can make an other env specific for that. Do you have any suggestions how to do this correctly? do we have a description of the right steps to follow to achieve this, a bit like the readme installation guide?

Can you be more specific? I had no problems of these sorts. What python version are you on, and which system (linux/windows)? Are you using poetry, or plain venv?

That's the venv I am using (pything 3.8.12):

Package                       Version     Editable project location
----------------------------- ----------- ----------------------------------------
alabaster                     0.7.12
aniso8601                     7.0.0
anyio                         3.5.0
argon2-cffi                   21.3.0
argon2-cffi-bindings          21.2.0
asciitree                     0.3.3
ase                           3.19.0
asteval                       0.9.26
astroid                       3.0.0
astropy                       5.0.2
asttokens                     2.0.5
atomicwrites                  1.4.0
attrs                         21.4.0
Babel                         2.12.1
backcall                      0.2.0
beautifulsoup4                4.11.1
bleach                        5.0.0
bokeh                         2.4.3
build                         0.10.0
CacheControl                  0.12.10
cachetools                    4.2.4
cachy                         0.3.0
cattrs                        22.2.0
certifi                       2021.10.8
cffi                          1.15.0
cfgv                          3.3.1
charset-normalizer            2.0.12
cleo                          2.0.1
click                         7.1.2
clikit                        0.6.2
cloudpickle                   2.0.0
commonmark                    0.9.1
contourpy                     1.0.6
coverage                      6.3.2
cramjam                       2.5.0
crashtest                     0.4.1
cryptography                  36.0.1
cycler                        0.11.0
Cython                        0.29.28
dask                          2023.3.0
debugpy                       1.5.1
decorator                     5.1.1
deepdish                      0.3.7
defusedxml                    0.7.1
diffpy.structure              3.1.0
diffsims                      0.5.1
dill                          0.3.5.1
distlib                       0.3.4
distributed                   2023.3.0
docstring-parser              0.12
docutils                      0.17.1
dtaidistance                  2.3.10
dulwich                       0.21.5
ecdsa                         0.17.0
elabapi-python                0.1.7
elasticsearch                 6.8.2
elasticsearch-dsl             6.4.0
entrypoints                   0.4
exceptiongroup                1.1.0
executing                     0.8.3
fabio                         0.13.0
fastdtw                       0.3.4
fasteners                     0.18
fastentrypoints               0.12
fastjsonschema                2.16.1
fastparquet                   0.8.3
ffmpeg                        1.4
ffmpeg-python                 0.2.0
filelock                      3.9.0
flake8                        4.0.1
flatdict                      4.0.1
fonttools                     4.30.0
fsspec                        2022.2.0
funcy                         1.17
future                        0.18.2
gitdb                         4.0.10
GitPython                     3.1.31
graphviz                      0.20.1
h11                           0.12.0
h5grove                       1.2.0
h5py                          3.8.0
HeapDict                      1.0.1
html5lib                      1.1
httpcore                      0.14.7
httpx                         0.22.0
hyperspy                      1.7.5
identify                      2.4.12
idna                          3.3
ifes-apt-tc-data-modeling     0.0.8
igor                          0.3
imageio                       2.16.1
imagesize                     1.3.0
importlib-metadata            4.13.0
importlib-resources           5.9.0
iniconfig                     2.0.0
installer                     0.7.0
ipykernel                     6.9.1
ipympl                        0.9.2
ipyparallel                   8.4.1
ipython                       8.6.0
ipython-genutils              0.2.0
ipywidgets                    7.7.2
isort                         4.3.21
jaraco.classes                3.2.3
jax                           0.4.4
jaxlib                        0.4.4
jedi                          0.18.1
jeepney                       0.7.1
Jinja2                        3.0.3
jmespath                      0.10.0
joblib                        1.2.0
json5                         0.9.11
jsonschema                    4.17.3
jupyter-client                7.1.2
jupyter_core                  5.2.0
jupyter-server                1.23.6
jupyterlab                    3.5.3
jupyterlab-h5web              7.0.0
jupyterlab-pygments           0.2.2
jupyterlab_server             2.22.1
jupyterlab-widgets            1.1.1
keyring                       23.13.1
kikuchipy                     0.8.4
kiwisolver                    1.4.0
lark                          1.1.5
lazy-object-proxy             1.7.1
llvmlite                      0.38.1
lmfit                         1.0.3
locket                        1.0.0
lockfile                      0.12.2
lxml                          4.7.1
MarkupSafe                    2.1.0
matplotlib                    3.6.2
matplotlib-inline             0.1.3
matplotlib-scalebar           0.8.1
mccabe                        0.6.1
mistune                       0.8.4
more-itertools                8.12.0
mpes                          1.1.3
mpmath                        1.2.1
msgpack                       1.0.3
mypy                          1.2.0
mypy-extensions               1.0.0
natsort                       8.2.0
nbclassic                     1.0.0
nbclient                      0.6.6
nbconvert                     6.5.3
nbformat                      5.4.0
nest-asyncio                  1.5.4
networkx                      2.7.1
nexusparser                   0.0.1
nodeenv                       1.6.0
nomad-lab                     1.0.3
nose                          1.3.7
notebook                      6.4.12
notebook_shim                 0.2.3
nptyping                      1.4.4
numba                         0.55.2
numcodecs                     0.11.0
numexpr                       2.8.1
numpy                         1.21.6
numpy-quaternion              2022.4.3
odfpy                         1.4.1
opencv-contrib-python         4.7.0.72
opt-einsum                    3.3.0
orix                          0.11.1
orjson                        3.6.0
packaging                     21.3
pandas                        1.5.1
pandocfilters                 1.5.0
parso                         0.8.3
partd                         1.2.0
pasha                         0.1.1
pastel                        0.2.1
pexpect                       4.8.0
photutils                     1.3.0
pickleshare                   0.7.5
Pillow                        9.0.1
Pint                          0.17
pip                           23.1.2
pkginfo                       1.9.6
pkgutil_resolve_name          1.3.10
platformdirs                  2.6.2
pluggy                        1.0.0
poetry                        1.4.2
poetry-core                   1.5.2
poetry-plugin-export          1.3.0
polars                        0.16.8
pooch                         1.7.0
pre-commit                    2.17.0
prettytable                   3.6.0
prometheus-client             0.14.1
prompt-toolkit                3.0.28
psutil                        5.9.3
ptyprocess                    0.7.0
pure-eval                     0.2.2
py                            1.11.0
pyaml                         21.10.1
pyarrow                       7.0.0
pyasn1                        0.4.8
PyCifRW                       4.4.5
pycodestyle                   2.8.0
pycparser                     2.21
pydantic                      1.8.2
pyerfa                        2.0.0.1
pyfai                         2023.3.0
pyflakes                      2.4.0
Pygments                      2.11.2
pylev                         1.4.0
pylint                        3.0.1
pylint-plugin-utils           0.5
pynxtools                     0.0.2
pyparsing                     3.0.7
pyproject_hooks               1.0.0
pyRestTable                   2020.0.3
pyrsistent                    0.18.1
pytest                        7.2.2
pytest-cov                    2.7.1
pytest-timeout                1.4.2
python-dateutil               2.8.2
python-jose                   3.3.0
python-json-logger            2.0.2
python-keycloak               0.26.1
pytz                          2021.1
PyWavelets                    1.2.0
pyxem                         0.15.0
PyYAML                        6.0
pyzmq                         22.3.0
radioactivedecay              0.4.17
rapidfuzz                     2.13.7
readme-renderer               35.0
recommonmark                  0.7.1
requests                      2.30.0
requests-cache                1.0.1
requests-toolbelt             0.9.1
rfc3986                       1.5.0
rich                          12.2.0
rsa                           4.8
scikit-image                  0.19.2
scikit-learn                  1.2.2
scipy                         1.9.3
SecretStorage                 3.3.1
sed                           0.1.0       /mnt/pcshare/users/Laurenz/AreaB/sed/sed
Send2Trash                    1.8.0
setuptools                    60.9.3
setuptools-scm                6.4.2
shellingham                   1.5.0.post1
silx                          1.1.0
six                           1.16.0
smmap                         5.0.0
sniffio                       1.2.0
snowballstemmer               2.2.0
sortedcontainers              2.4.0
soupsieve                     2.3.2.post1
sparse                        0.14.0
Sphinx                        4.4.0
sphinx-autodoc-typehints      1.17.0
sphinx-rtd-theme              1.0.0
sphinxcontrib-applehelp       1.0.2
sphinxcontrib-devhelp         1.0.2
sphinxcontrib-htmlhelp        2.0.0
sphinxcontrib-jsmath          1.0.1
sphinxcontrib-qthelp          1.0.3
sphinxcontrib-serializinghtml 1.1.5
stack-data                    0.2.0
structlog                     21.5.0
symmetrize                    0.5.5
sympy                         1.11.1
tables                        3.7.0
tblib                         1.7.0
terminado                     0.15.0
threadpoolctl                 3.1.0
tifffile                      2022.10.10
tinycss2                      1.1.1
toml                          0.10.2
tomli                         2.0.1
tomlkit                       0.11.4
toolz                         0.11.2
tornado                       6.1
tqdm                          4.64.1
traitlets                     5.3.0
traits                        6.4.1
transforms3d                  0.4.1
trove-classifiers             2023.2.20
twine                         4.0.0
typed-ast                     1.4.3
types-PyYAML                  6.0.12.1
types-requests                2.31.0.1
types-urllib3                 1.26.25.13
typing_extensions             4.1.1
typish                        1.9.3
uncertainties                 3.1.6
url-normalize                 1.4.3
urllib3                       1.26.8
virtualenv                    20.13.3
wcwidth                       0.2.5
webencodings                  0.5.1
websocket-client              1.5.1
wheel                         0.37.1
widgetsnbextension            3.6.1
wrapt                         1.13.3
xarray                        0.20.2
xyzservices                   2022.9.0
zarr                          2.14.2
zict                          2.1.0
zipfile37                     0.1.3
zipp                          3.7.0

@rettigl
Copy link
Member

rettigl commented Oct 12, 2023

Hopefully the end result is to have a unified method, with features from both worlds working as expected.

Great, just wanted to make sure we are on the same track.

@steinnymir
Copy link
Member Author

That's the venv I am using (pything 3.8.12):

did you install this with Poetry? what steps did you follow?
I did poetry installation on python 3.9
then conda (or pip, I'm not sure now) install pre-commit created conflicts...

I'll try install py38, poetry and then pip install pre-commit==2.17.0

@rettigl
Copy link
Member

rettigl commented Oct 26, 2023

I think the issue with the calibration is still with the correct values for the time of flight. For a TOF length of ~1 m, and a drift energy of ~20-30 eV, you expect TOF values of ~300-350 ns. The data show more 600-800 ns. This just does not fit.

@rettigl
Copy link
Member

rettigl commented Oct 26, 2023

image
image

@steinnymir
Copy link
Member Author

I think the issue with the calibration is still with the correct values for the time of flight. For a TOF length of ~1 m, and a drift energy of ~20-30 eV, you expect TOF values of ~300-350 ns. The data show more 600-800 ns. This just does not fit.

I remember we had discussions at FLASH about which was the correct range for the ToF. I don't remember what the conclusion was, but I know we decided to represent ToF in the ~700ns range. The difference is only a factor 2 on the step size I believe, so we could test if this helps the energy calibration.

It might fit though that the tof distance results in being about sqrt(2) times larger...

@steinnymir
Copy link
Member Author

image image

are these obtained changing the tof_binsize?
Error bars are huge though, and d should be somewhere around 1- 1.2m. The t0 seems correct instead.

@rettigl
Copy link
Member

rettigl commented Oct 26, 2023

This uses the very same settings, just leaves more freedom to t0. the photon peak is at 0.2E-7 or so, i.e. very far off. I think one should not expect a perfect agreement to physical values, but take this as an empirical calibration. after all, electrons don't have a constant energy in the instrument.

@rettigl
Copy link
Member

rettigl commented Oct 26, 2023

Correlations are very large, that's why the error bars are so large. Maybe one should fix one of values best, e.g. the d.

Copy link
Member

@rettigl rettigl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went through the code and tried to review and comment as much as I could, but without unit tests, it's really hard to verify the more compley functions. I strongly encourage you to write tests before merging.

sed/calibrator/energy.py Show resolved Hide resolved
sed/calibrator/energy.py Show resolved Hide resolved
sed/calibrator/energy.py Show resolved Hide resolved
sed/calibrator/energy.py Outdated Show resolved Hide resolved
sed/calibrator/energy.py Outdated Show resolved Hide resolved
sed/core/processor.py Outdated Show resolved Hide resolved
sed/core/processor.py Outdated Show resolved Hide resolved
sed/core/processor.py Outdated Show resolved Hide resolved
sed/loader/utils.py Show resolved Hide resolved
tests/data/loader/flash/config.yaml Outdated Show resolved Hide resolved
@rettigl
Copy link
Member

rettigl commented Oct 28, 2023

Further comments to the Notebook:

  • I typically did the energy calibration with peaks, and not derivatives. That's what the code is optimized for, and that is what is to my opinion more stable whith chaning bias. Slopes might change, peak position should not. So I would discourage what you are doing.
  • The fitting seems to be rather unstable, even if you leave the parameters free to find reasonable values. I did not observe this so far, but we typically use much larger ranges of biases, typically +-5 or 6 V, i.e. 10-12 traces. This makes everything much more stable. After all, fitting 3 correlated parameters in a non-linear fit with 5 data points is not so reliable...
  • apply_energy_axis: As I noted in the review, it does not work if you supply sign as a single integer
  • smooth_columns: Does not work at all (throws KeyError)
  • Fit verification of energy calibration: I would again not fit the derivative, but either fit the Fermi edge with a Fermi function, or fit the peak with a peak (better). The slope is influenced by the resolution, which changes somewhat with bias, and is thus not reliable. Look at your fits, they are not good fits.
  • Load and process from config: This does not work for me, because it load the sed_config.yaml file in the tutorial folder, which has been commited there accidentially perviously. Thus, it uses the wrong energy correction. I suggest removing this file with this PR, and add a call to save_energy_calibration into the notebook, in addition to removing the energy calibration from the hextof_config.yaml file.

@steinnymir
Copy link
Member Author

To answer your general comments:

I typically did the energy calibration with peaks, and not derivatives. That's what the code is optimized for, and that is what is to my opinion more stable whith chaning bias. Slopes might change, peak position should not. So I would discourage what you are doing.

This is correct, I tried with the derivative as it is sharper than the peak, and showed better performance than using a broad noisy peak.

The fitting seems to be rather unstable, even if you leave the parameters free to find reasonable values. I did not observe this so far, but we typically use much larger ranges of biases, typically +-5 or 6 V, i.e. 10-12 traces. This makes everything much more stable. After all, fitting 3 correlated parameters in a non-linear fit with 5 data points is not so reliable...

I agree we need more points, this is something we'll bring up during beamtime. But I find the instability of the fit is also related to the simplistic peakfind method, as we already discussed.

apply_energy_axis: As I noted in the review, it does not work if you supply sign as a single integer

worked as intended, where you must explicitly mention the sign of each column.

smooth_columns: Does not work at all (throws KeyError)

removed functionality entirely.

Fit verification of energy calibration: I would again not fit the derivative, but either fit the Fermi edge with a Fermi function, or fit the peak with a peak (better). The slope is influenced by the resolution, which changes somewhat with bias, and is thus not reliable. Look at your fits, they are not good fits.

those are not intended as good fits, but just help show how bad the calibration performed. I agree with the point about the derivative, but as before, using the maxima of a (not fitted) wide peak is not better.

Load and process from config: This does not work for me, because it load the sed_config.yaml file in the tutorial folder, which has been commited there accidentially perviously. Thus, it uses the wrong energy correction. I suggest removing this file with this PR, and add a call to save_energy_calibration into the notebook, in addition to removing the energy calibration from the hextof_config.yaml file.

I'm not sure what you mean, but this might be related to a note I made before, where you must delete the old file, it is a security breach. Ideally you should delete your repo and clone it fresh, as the git history has changed and you risk re-introducing it in the history.

I never looked into the save_energy_calibration method, I'll have a look and add it if possible.

@rettigl
Copy link
Member

rettigl commented Nov 1, 2023

I'm not sure what you mean, but this might be related to a note I made before, where you must delete the old file, it is a security breach. Ideally you should delete your repo and clone it fresh, as the git history has changed and you risk re-introducing it in the history.

What I mean is this file:

calibration:

This is automatically loaded, and contains the mpes energy calibration saved by the tutorial notebook 2. It was in my case used, because I loaded the created config file as user config rather as config, to be able to overwrite the paths. This, however, made that folder_config file overwrite the energy calibration in the user_config...
It's a specific problem of the way I included the config, I realize now.

@steinnymir
Copy link
Member Author

most new functions should be tested now, and what is not should get an issue and will be dealt with later. I am happy with the state of this PR and would like to merge. @rettigl I need your blessing to do so! :)

Copy link
Member

@rettigl rettigl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the tests this looks quite reasonable now. I think all my points from before are addressed. I did not look at everything again now, but think you can merge it. There are a few points that you can still address if you want (minor).

sed/calibrator/energy.py Outdated Show resolved Hide resolved
sed/calibrator/energy.py Outdated Show resolved Hide resolved
sed/calibrator/energy.py Show resolved Hide resolved
sed/calibrator/energy.py Outdated Show resolved Hide resolved
sed/core/dfops.py Outdated Show resolved Hide resolved
@steinnymir steinnymir merged commit 35bd293 into main Nov 2, 2023
3 checks passed
@steinnymir steinnymir deleted the hextof_workflow_steps branch November 2, 2023 13:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants